---@class UIGuildTask : UIBase
UIGuildTask = UIBase.New(PanelResNames.UIGuildTask)
local t = UIGuildTask

function t.Init(func)
    UIBase.Init(t, func)
end

function t:OnCreate()
    self.taskInfoList = {}
    self.btn_QuickGet = self:GetChild("btn_QuickGet")
    self.btnBack = self:GetChild("btnBack")
    self.list_task = self:GetList("list_task")
    self.list_gift = self:GetList("list_gift")
    self.totalText = self:GetChild("TotaltaskInfo")
    --local giftRoot = list_gift:GetChildAt(0)
    --self.livenessBar = giftRoot:GetChild("n0")
    --self.livenessItemList = {}
    --for i = 1,10 do
    --    local gift = giftRoot:GetChild("n"..i)
    --    if gift then
    --        local status = gift:GetController("status")
    --        local numText = gift:GetChild("num")
    --        local boxCfg = dataTable.getDataTableByStringId("t_guildtaskbox",i)
    --        if boxCfg then
    --            numText.text = tostring(boxCfg.f_NeedExpCount)
    --        end
    --        self:RegisterOnClick(gift,self.Clickliveness,self,i)
    --        table.insert(self.livenessItemList,{status = status,numText = numText})
    --    end
    --end
    self:SetDynamicList(self.list_task,self.TaskItemRender)
    self:SetDynamicList(self.list_gift,self.BoxItemRender)

    self:RegisterOnClick(self.btn_QuickGet,self.ClickQuickGet)
    self:RegisterOnClick(self.btnBack,self.ClickClose)
    self:RegisterEvent(Config.EventType.Fresh_Guild_Task, self.Refresh)
    self.poolMgr = ComponentPoolMgr.New(t.TaskItem)
    self:Refresh()
end
function t:OnEnable()
end
function t:OnDisable()
end
function t:OnDestroy()
    self.poolMgr:OnDestroy()
    self.poolMgr = nil
    self.taskInfoList = nil
end
function t:Refresh()
    self:RefreshTask()
    self:RefreshLiveness()
end
function t:RefreshTask()
    logicMgr.GuildManager.GetTaskInfoList(self.taskInfoList,true)
    self.list_task.numItems = #self.taskInfoList
end
function t:RefreshLiveness()
    local count = logicMgr.GuildManager.taskLivenessCount
    --for k,v in pairs(self.livenessItemList) do
    --    local recieved = logicMgr.GuildManager.IsLivenessRewardRecieved(k)
    --    if recieved then
    --        v.status.selectedIndex = 2
    --    else
    --        local cfg = dataTable.getDataTableByStringId("t_guildtaskbox",k)
    --        if cfg and cfg.f_NeedExpCount <= count then
    --            v.status.selectedIndex = 1
    --        else
    --            v.status.selectedIndex = 0
    --        end
    --    end
    --end
    --self.livenessBar.value = count
    self.totalText.text = tostring(count)
    self.boxCfg = G.dataTable["t_guildtaskbox"]
    self.boxCfgCount = 0
    for k,v in pairs(self.boxCfg) do
        self.boxCfgCount = self.boxCfgCount + 1
    end
    self.list_gift.numItems = self.boxCfgCount
end
function t:Clickliveness(index)
    local recieved = logicMgr.GuildManager.IsLivenessRewardRecieved(index)
    if recieved then
        return
    end
    local cfg = dataTable.getDataTableByStringId("t_guildtaskbox",index)
    if not cfg then return end
    if cfg.f_NeedExpCount <= logicMgr.GuildManager.taskLivenessCount then
        unionHandler.sendReqUnionTaskBoxAward(cfg.f_BoxID)
    else
        logicMgr.UIShowManager.ShowRewardPreviewByJson(cfg.f_BoxAward)
    end
end
function t:TaskItemRender(index,go)
    ---@type UIGuildTaskItem
    local taskItem = self.poolMgr:GetBindScript(go)
    local data = self.taskInfoList[index]
    taskItem:Refresh(data)
end

function t:BoxItemRender(index, go)
    local progress = go:GetChild("n0")
    local boxStateCtrl = go:GetChild("n1"):GetController("status")
    local txtNum = go:GetChild("n1"):GetChild("num")
    local cfgData = self.boxCfg[tostring(index)]
    txtNum.text = cfgData.f_NeedExpCount
    local score = logicMgr.GuildManager.taskLivenessCount
    local scorePerBox = 100/(self.boxCfgCount)
    local value = score - (index-1)*scorePerBox
    if value > scorePerBox then
        value = 100
    end
    progress.value = (value/scorePerBox)*100
    go:GetChild("n1").onClick:Clear()
    if score >= cfgData.f_NeedExpCount then
        if logicMgr.GuildManager.CheckEleInList(index, logicMgr.GuildManager.taskLivenessList) then
            boxStateCtrl.selectedIndex = 2  --已领取

        else
            boxStateCtrl.selectedIndex = 1  --可领取
            go:GetChild("n1").onClick:Add(function()
                unionHandler.sendReqUnionTaskBoxAward(cfgData.f_BoxID)
            end)
        end
    else
        boxStateCtrl.selectedIndex = 0  --未完成
        go:GetChild("n1").onClick:Add(function()
            logicMgr.UIShowManager.ShowRewardPreviewByJson(cfgData.f_BoxAward)
        end)
    end

end

function t:ClickQuickGet()
    if logicMgr.GuildManager.CheckHasAwardGet() then
        unionHandler.sendReqAllGuildTaskAward()
    else
        UISysTips.AddMsg(getLanguage("TaskInfo_btnQFtips"))
    end
end
function t:ClickClose()
    closeUI(PanelNames.UIGuildTask)
end
t.TaskItem = Class("",UIComponentInterface)
---@class UIGuildTaskItem:UIComponentInterface
local TaskItem = t.TaskItem
function TaskItem:OnCreate()
    self.ctrlType = self:GetController("type")
    self.nameText = self:GetChild("TaskName")
    self.bar = self:GetChild("bar"):GetChild("bar")
    self.barText = self:GetChild("scheduleText")
    self.item = self:GetChild("Fr1")
    self.btn_Get = self:GetChild("btn_Get")
end
function TaskItem:Refresh(data)
    local cfg = dataTable.getDataTableByStringId("t_guildtask",data.f_GuildTaskId)
    if not cfg then
        logError("找不到该任务："..data.taskId)
        return
    end
    self.nameText.text = getLanguage(cfg.f_GuildTaskDesc)
    if data.status == 0 then    --0:可领取 1:未完成 2:已领取
        self.ctrlType.selectedIndex = 1
    elseif data.status == 1 then
        self.ctrlType.selectedIndex = 0
    elseif data.status == 2 then
        self.ctrlType.selectedIndex = 2
    end
    self.bar.fillAmount = data.progress/cfg.f_Progress
    self.barText.text = data.progress.."/"..cfg.f_Progress
    local ctrl = CtrlManager.GetCtrl(PanelNames.Item)
    ctrl.initData(self.item,{itemModelId = CurrencyEnum.GuildScore,num = cfg.f_ExpCount})
    self.btn_Get.onClick:Clear()
    self.btn_Get.onClick:Add(function()
        unionHandler.sendReqUnionTaskAward(data.f_GuildTaskId)
    end)
end
return t